﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace dmp_ui_demo
{
    public delegate void EditorDataSave(string identity, string title, string content);
    /// <summary>
    /// 用户在编辑器的WebBrowser对象和程序之间相互调用的类
    /// </summary>
    [System.Runtime.InteropServices.ComVisible(true)]
    public class Bridge
    {
        /// <summary>
        /// 用于在WebBrowser中判断当前是否在调试模式
        /// </summary>
        public bool IsDebug { get; private set; }

        /// <summary>
        /// 浏览器对象
        /// </summary>
        public WebBrowser Browser { get; private set; }

        /// <summary>
        /// 编辑器是否只读
        /// </summary>
        public bool ReadOnly { get; private set; }

        private string templateUrl = string.Empty;

        public event EditorDataSave OnDataSave;

        public Bridge(WebBrowser browser, bool readOnly)
        {
            this.Browser = browser;
            this.Browser.ObjectForScripting = this;
            this.Browser.DocumentCompleted += Browser_DocumentCompleted;
            this.ReadOnly = readOnly;
            //templateUrl = AppContext.SysConfig["EditorUrl"];
#if DEBUG
            IsDebug = true;
            templateUrl = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\ckeditor\template.htm"));
#endif
            loadTemplate();
        }

        public void Refresh()
        {
            loadTemplate();
        }

        void Browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void loadTemplate()
        {
            Browser.Url = new Uri(templateUrl);
        }

        public void AddParagraph(string identity, string title, string content)
        {
            var element = Browser.Document.CreateElement("div");
            element.InnerHtml = "test";
            Browser.Document.Body.AppendChild(element);
        }

        public void DataSave()
        {
            OnDataSave(string.Empty, string.Empty, string.Empty);
        }
    }
}
